package com.zhao.service.impl;

import com.zhao.model.Fruit;
import com.zhao.service.PromotionStrategy;
import com.zhao.uitl.Utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;

// 满减促销
public class FullReductionPromotion implements PromotionStrategy {

    private final BigDecimal fullAmount;

    private final BigDecimal reductionAmount;

    public FullReductionPromotion(BigDecimal fullAmount, BigDecimal reductionAmount) {
        this.fullAmount = fullAmount;
        this.reductionAmount = reductionAmount;
    }

    @Override
    public BigDecimal calculate(Map<Fruit, Integer> fruitQuantities) {
        BigDecimal totalPrice = Utils.getTotalPrice(fruitQuantities);
        BigDecimal reduction = totalPrice.divide(fullAmount, 0, RoundingMode.DOWN).multiply(reductionAmount);
        totalPrice = totalPrice.subtract(reduction);
        return totalPrice.setScale(2, RoundingMode.HALF_UP);
    }

    @Override
    public int getPriority() {
        return 1; // 满减策略优先级最高
    }
}
